package com.wanli.agent.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.DeviceUnifyNewBean;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.widget.AppTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 机具
 */
public class MachinesActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_total_num)
    TextView tvTotalNum;
    @BindView(R.id.tv_setting1)
    TextView tvSetting1;
    @BindView(R.id.tv_setting2)
    TextView tvSetting2;
    @BindView(R.id.tv_setting3)
    TextView tvSetting3;
    @BindView(R.id.tv_setting4)
    TextView tvSetting4;
    @BindView(R.id.tv_type_1_count)
    TextView tvType1Count;
    @BindView(R.id.tv_type_3_count)
    TextView tvType3Count;
    @BindView(R.id.tv_type_4_count)
    TextView tvType4Count;
    @BindView(R.id.tv_type_6_count)
    TextView tvType6Count;
    @BindView(R.id.tv_type_8_count)
    TextView tvType8Count;
    @BindView(R.id.tv_setting5)
    TextView tvSetting5;
    @BindView(R.id.tv_type_7_count)
    TextView tvType7Count;
    @BindView(R.id.tv_setting6)
    TextView tvSetting6;
    private int child_agent_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machines);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null) {
            child_agent_id = userInfoBean.getData().getId();
        }
        initData();
    }

    public void initData() {
        new HomePageModelImpl().requestChildDeviceStatistics(child_agent_id + "", new DataCallBack<DeviceUnifyNewBean>() {
            @Override
            public void onSuccessful(DeviceUnifyNewBean bean) {
                int totalNum = 0;
                for (DeviceUnifyNewBean.DataBean dataBean : bean.getData()) {
                    if (8 == dataBean.getType()) {
                        totalNum = totalNum + dataBean.getCount();
                        tvType8Count.setText(dataBean.getCount() + "(张)");
                    } else if (6 == dataBean.getType()) {
                        totalNum = totalNum + dataBean.getCount();
                        tvType6Count.setText(dataBean.getCount() + "(台)");
                    } else if (4 == dataBean.getType()) {
                        totalNum = totalNum + dataBean.getCount();
                        tvType4Count.setText(dataBean.getCount() + "(台)");
                    } else if (3 == dataBean.getType()) {
                        totalNum = totalNum + dataBean.getCount();
                        tvType3Count.setText(dataBean.getCount() + "(张)");
                    } else if (1 == dataBean.getType()) {
                        totalNum = totalNum + dataBean.getCount();
                        tvType1Count.setText(dataBean.getCount() + "(台)");
                    }
                }
                tvTotalNum.setText(totalNum + "");
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_setting1, R.id.tv_setting2, R.id.tv_setting3, R.id.tv_setting4, R.id.tv_setting5, R.id.tv_setting6})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_setting1:
                Intent intent = new Intent(mActivity, MachinesDetailsActivity.class);
                intent.putExtra("type", "1");
                intent.putExtra("child_agent_id", child_agent_id + "");
                intent.putExtra("agent_name", "");
                startActivity(intent);
                break;
            case R.id.tv_setting2:
                intent = new Intent(mActivity, MachinesDetailsActivity.class);
                intent.putExtra("type", "3");
                intent.putExtra("child_agent_id", child_agent_id + "");
                intent.putExtra("agent_name", "");
                startActivity(intent);
                break;
            case R.id.tv_setting3:
                intent = new Intent(mActivity, MachinesDetailsActivity.class);
                intent.putExtra("type", "4");
                intent.putExtra("child_agent_id", child_agent_id + "");
                intent.putExtra("agent_name", "");
                startActivity(intent);
                break;
            case R.id.tv_setting4:
                intent = new Intent(mActivity, MachinesDetailsActivity.class);
                intent.putExtra("type", "6");
                intent.putExtra("child_agent_id", child_agent_id + "");
                intent.putExtra("agent_name", "");
                startActivity(intent);
                break;
            case R.id.tv_setting5:
                intent = new Intent(mActivity, MachinesDetailsActivity.class);
                intent.putExtra("type", "8");
                intent.putExtra("child_agent_id", child_agent_id + "");
                intent.putExtra("agent_name", "");
                startActivity(intent);
                break;
            case R.id.tv_setting6:
                intent = new Intent(mActivity, MachinesDetailsActivity.class);
                intent.putExtra("type", "5");
                intent.putExtra("child_agent_id", child_agent_id + "");
                intent.putExtra("agent_name", "");
                startActivity(intent);
                break;
        }
    }
}
